import logging
import requests

from config import app_path
from log.logging_use import init_log_config


class IhrmApi(object):
    token = None
    add_id = None

    @classmethod
    def login(cls, session, login_json):
        """
        :param session:
        :param login_json:
        :return:登录接口
        """
        url = "http://ihrm.itheima.net/prod-api/sys/login"
        logging.info(f"login_url:{url}")
        resp = session.post(url=url, json=login_json)
        # 当登录失败时没有data就不执行这个操作也就没有返回的cls.token值
        # 因此token直接为0就好了
        if resp.json().get("data") is not None:
            cls.token = "Bearea " + resp.json()["data"]
            logging.info(f"token:{cls.token}")
        else:
            cls.token = None
            logging.info(f"token:None")
        logging.info(f"login_resp:{resp.json()}")
        return resp

    @classmethod
    def add_user(cls, session, add_json):
        """
        :param session:
        :param add_json:
        :return:添加员工接口
        """
        url = "http://ihrm.itheima.net/prod-api/sys/user"
        logging.info(f"add_url:{url}")
        header = {"Authorization": cls.token}
        logging.info(f"Authorization:{cls.token}")
        resp_add = session.post(url=url, headers=header, json=add_json)
        if resp_add.json().get("data") is not None:
            cls.add_id = resp_add.json().get("data").get("_id")
            logging.info(f"emp_id:{cls.add_id}")
        else:
            cls.add_id = None
            logging.info(f"emp_id:None")
        logging.info(f"resp:{resp_add.json()}")
        return resp_add

    @classmethod
    def delt_user(cls, session, delt_json):
        """
        :param session:
        :param delt_json:
        :return:删除员工接口
        """
        user_id = delt_json
        url = "http://ihrm.itheima.net/prod-api/sys/user/" + user_id
        header = {"Authorization": cls.token}
        logging.info(f"url:{url}")
        logging.info(f"header:{header}")
        resp_delt = session.delete(url=url, headers=header)
        logging.info(f"resp_delt:{resp_delt.json()}")
        return resp_delt

    @classmethod
    def upd_user(cls, session, upd_json):
        """
        :param session:
        :param upd_json:
        :return:修改员工接口
        """
        url = "http://ihrm.itheima.net/prod-api/sys/user/" + "604f764971f93f3ac8f365c2"
        logging.info(f"url:{url}")
        header = {"Authorization": cls.token}
        logging.info(f"Authorization:{cls.token}")
        resp_upd = session.put(url=url, headers=header, json=upd_json)
        logging.info(f"resp_upd:{resp_upd.json()}")
        return resp_upd

    @classmethod
    def sel_users(cls, session, param_data):
        """
        :param session:
        :param param_data:
        :return:查询员工信息接口
        """
        url = "http://ihrm.itheima.net/prod-api/sys/user"
        logging.info(f"url:{url}")
        param = param_data
        header = {"Authorization": cls.token, "Accept": "application/json, text/plain, */*"}
        logging.info(f"header:{header}")
        resp_sel = session.get(url=url, headers=header, params=param)
        logging.info(f"resp_sel:{resp_sel.json()}")
        return resp_sel

    @classmethod
    def sel_user(cls, session, param_data):
        """
        :param session:
        :param param_data:
        :return:查询员工信息接口
        """
        param = param_data
        logging.info(f"param:{param_data}")
        url = "http://ihrm.itheima.net/prod-api/sys/user/" + param
        logging.info(f"url:{url}")
        header = {"Authorization": cls.token, "Accept": "application/json, text/plain, */*"}
        resp_sel = session.get(url=url, headers=header, params=param)
        logging.info(f"{resp_sel.json()}")
        return resp_sel


# if __name__ == '__main__':
#     init_log_config(app_path + "/log/ihrm_log.log")
#     session = requests.Session()
#     # 登录接口
#     login_json = {"mobile": "13800000002", "password": "123456"}
#     resp = IhrmApi.login(session, login_json)
#     logging.info("登录接口运行完成")
#     # 添加员工接口
#     add_json = {"username": "大猪乔治", "mobile": "13899078431", "timeOfEntry": "2021-12-01", "formOfEmployment": 1,
#                 "workNumber": "777888", "departmentName": "测试", "departmentId": "1452603344685203456",
#                 "correctionTime": "2021-12-30T16:00:00.000Z"}
#     resp_add = IhrmApi.add_user(session, add_json)
#     logging.info("添加接口运行完成")
#     # 修改员工信息接口
#     upd_json = {"roleIds": [],
#                 "_id": "604f764971f93f3ac8f365c2",
#                 "username": "无敌暴龙战神",
#                 "mobile": "13800000002",
#                 "timeOfEntry": "2021-12-01",
#                 "formOfEmployment": "1",
#                 "workNumber": "777888",
#                 "departmentName": "测试",
#                 "departmentId": "1452603344685203456",
#                 "correctionTime": "2021-12-30T16:00:00.000Z",
#                 "password": "e10adc3949ba59abbe56e057f20f883e"}
#     resp_upd = IhrmApi.upd_user(session, upd_json)
#     logging.info("修改信息接口运行完成")
#     # 删除员工接口
#     resp_delt = IhrmApi.delt_user(session, IhrmApi.add_id)
#     logging.info("删除接口运行完成")
#     param_data = {"total": 0, "page": 1, "size": 10}
#     resp_sel = IhrmApi.sel_users(session, param_data)
#     logging.info("查询接口运行完成")
